package com.dgz.springboot18.service.impl;

import com.dgz.springboot18.mapper.StudentMapper;
import com.dgz.springboot18.mapper.StudentTaskMapper;
import com.dgz.springboot18.mapper.TaskMapper;
import com.dgz.springboot18.pojo.AdminInfo;
import com.dgz.springboot18.pojo.StudentInfo;
import com.dgz.springboot18.pojo.StudentTask;
import com.dgz.springboot18.pojo.TaskInfo;
import com.dgz.springboot18.service.TaskService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StudentTaskMapper studentTaskMapper;

    @Autowired
    private StudentMapper studentMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private Integer taskId=1000;
    /**
     * 添加任务
     * @param taskInfo
     */
    @Override
    public TaskInfo addTask(TaskInfo taskInfo){
        AdminInfo adminInfo = (AdminInfo) SecurityUtils.getSubject().getPrincipal();
        Integer time=Integer.parseInt(taskInfo.getTaskAndTime());
        this.taskId=this.taskId+1;
        taskInfo.setTaskId(String.valueOf(this.taskId));
        taskInfo.setTaskAdmin(adminInfo.getAdminName());
        Date date=new Date();
        taskInfo.setTaskStartTime(sdf.format(date));
        date.setTime(date.getTime() + time * 60 * 1000);
        taskInfo.setTaskAndTime(sdf.format(date));
        taskInfo.setStatus(0);
        taskInfo.setIsDel(0);

        //在添加任务的同时,把这个任务发布给每一个学生
        for (int i=0;i<=100;i++){
            StudentTask studentTask=new StudentTask("100"+i,String.valueOf(this.taskId),false,null);
            studentTaskMapper.save(studentTask);
        }
        return taskMapper.save(taskInfo);
    }

    /**
     * 根据id删除任务
     * @param id
     */
    @Override
    public void delTask(String id){
        taskMapper.deleteById(id);
    }

    /**
     * 根据id修改任务
     * @param taskInfo
     */
    @Override
    public TaskInfo updTask(TaskInfo taskInfo){
        return taskMapper.save(taskInfo);
    }

    /**
     * 根据id查询任务
     * @param id
     * @return
     */
    @Override
    public TaskInfo queryTask(String id){
        Optional<TaskInfo> result = taskMapper.findById(id);
        return result.isPresent()?result.get():null;
    }

    /**
     * 全查询任务并且分页
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<TaskInfo> queryTasks(Integer page,Integer size){
        return taskMapper.findAll(PageRequest.of(page-1,size));
    }

    /**
     * 查询最新任务的学生打卡信息
      * @param studentTask
     * @param page
     * @param size
     * @return
     */
    @Override
   public Page<StudentTask> queryLast(StudentTask studentTask,Integer page,Integer size){
        String taskid=this.taskId==1000?String.valueOf(this.taskId+1):String.valueOf(this.taskId);
        studentTask.setTaskId(taskid);//String.valueOf(this.taskId
        return studentTaskMapper.findAll(Example.of(studentTask),PageRequest.of(page-1,size));
   }

    /**
     * 根据id查询学生信息
     * @return
     */
    @Override
    public StudentInfo queryStu(String id){
        Optional<StudentInfo> result = studentMapper.findById(id);
        return result.isPresent()?result.get():null;
    }


    /**
     * 根据任务id查询学生打卡信息
     * @param studentTask
     * @param page
     * @param size
     * @return
     */
    @Override
   public Page<StudentTask> queryLastById(StudentTask studentTask,Integer page,Integer size){
        return studentTaskMapper.findAll(Example.of(studentTask),PageRequest.of(page-1,size));
   }


}
